package com.taiyi.renaissancemall.ui.mine.order.orderExpress;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.OrderExpressListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

//查看物流页面，需传订单id
public class OrderExpressActivity extends BaseActivity<OrderExpressView, OrderExpressPresent> implements OrderExpressView {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.tv_expressName)
    TextView tvExpressName;
//    @BindView(R.id.tv_expressNum)
//    TextView tvExpressNum;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    private String ocId = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_express;
    }

    @Override
    public OrderExpressPresent initPresenter() {
        return new OrderExpressPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        titleTv.setText(getString(R.string.orderExpress));
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        ocId = getIntent().getStringExtra("ocId");
        presenter.getOrderExpress(ocId);
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void getOrderExpressSuccess(OrderExpressListBean orderExpressListBean) {
        String showText = "";
        if ("zhongtong".equals(orderExpressListBean.orderExpress.sendcode)) {
            showText = "中通快递：" + orderExpressListBean.orderExpress.sendexpressageno;
        } else {
            showText = orderExpressListBean.orderExpress.sendexpressagename + ":" + orderExpressListBean.orderExpress.sendexpressageno;
        }
        tvExpressName.setText(showText.trim());
//        tvExpressNum.setText(orderExpressListBean.orderExpress.sendexpressageno);
        if ("500".equals(orderExpressListBean.orderExpress.express.status)) {
            noDataIv.setVisibility(View.VISIBLE);
            ToastUtils.showLongToast(orderExpressListBean.orderExpress.express.message);
        } else {
            noDataIv.setVisibility(View.GONE);
            setAdapter(orderExpressListBean.orderExpress.express.data);
        }
    }

    private void setAdapter(List<OrderExpressListBean.OrderExpressBean.ExpressBean.DataBean> data) {
        OrderExpressAdapter orderExpressAdapter = new OrderExpressAdapter(R.layout.layout_order_express, data);
        recyclerView.setLayoutManager(new LinearLayoutManager(OrderExpressActivity.this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(orderExpressAdapter);
    }

    @Override
    public void getOrderExpressFailure(String msg) {
        ToastUtils.showLongToast(msg);
    }

    @OnClick({R.id.backIv, R.id.tv_copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.tv_copy:
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setText(tvExpressName.getText());
                ToastUtils.showLongToast("复制成功");
                break;
        }
    }
}
